package com.zpark.it.controller;

import com.zpark.it.param.AdminUserParam;
import com.zpark.it.pojo.AdminUser;
import com.zpark.it.pojo.User;
import com.zpark.it.service.AdminUserService;
import com.zpark.it.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Title: AdminUserController 后台登录用户处理
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/26 21:35
 */
@RestController
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    @PostMapping("/user/login")

    public R login(@Validated AdminUserParam adminUserParam, BindingResult result, HttpSession session){
        if (result.hasErrors()) {
            return  R.fail("账号为空，登录失败");
        }
        //验证码验证
        String captcha = (String) session.getAttribute("captcha");
        if (!adminUserParam.getVerCode().equalsIgnoreCase(captcha))
            return R.fail("验证码错误");

        AdminUser user = adminUserService.login(adminUserParam);
        if (user==null) {
            return R.fail("登录失败，账号或者密码错误");
        }

        session.setAttribute("userInfo",user);
        return R.ok("登录成功");
    }

    @GetMapping("user/logout")
    public R logout(HttpSession session){
        //清空session
        session.invalidate();//把所有session都清空

        return R.ok("退出登录成功");

    }

}
